<template>
	<div class="pages">
		<p-page-header title="生成并发测试文件"></p-page-header>
		<el-form size="mini" ref="formData" :model="formData" :rules="rules" label-width="auto">
			<el-form-item label="生成个数" prop="count">
				<el-input v-model.number="formData.count" type="number" max="40" min="10" clearable></el-input>
			</el-form-item>
			<el-form-item label="活动名称" prop="teamId">
				<el-select v-model="formData.teamId">
					<el-option v-for="item in list" :key="item.id" :value="item.id" :label="item.name"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" :loading="loading" @click="handleDownload">生成</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import {
		login
	} from '@/api/shop-user-api.js'
	import {
		getTeamListAlive
	} from '@/api/team-api.js'
	export default {
		data() {
			return {
				formData: {
					count: 10,

				},
				queryForm: {

				},
				loading: false,
				baseUserName: '189450519',
				basePassword: '123456',
				rules: {
					count: [{
						required: true,
						validator(rules, value, callback) {
							if (value < 10) {
								callback(new Error('最小生成10个'))
								return
							}
							if (value > 40) {
								callback(new Error('最小生成40个'))
								return
							}
							callback()
						}
					}],
					teamId: [{
						required: true,
						message: '必须选择一个活动'
					}]
				},
				list: [],
				baseTemplate: {
					"info": {
						"_postman_id": "eeeeee",
						"name": "并发测试文件",
						"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
					},
					"item": [

					]
				},
				itemTemplate: {
					"name": "活动并发测试接口",
					"protocolProfileBehavior": {
						"disableBodyPruning": true
					},
					"request": {
						"auth": {
							"type": "bearer",
							"bearer": [{
								"key": "token",
								"value": "",
								"type": "string"
							}]
						},
						"method": "POSt",
						"header": [{
							"warning": "This is a duplicate header and will be overridden by the Content-Type header generated by Postman.",
							"key": "Content-Type",
							"value": "application/x-www-form-urlencoded",
							"type": "text"
						}],
						"body": {
							"mode": "urlencoded",
							"urlencoded": []
						},
						"url": {
							"raw": "http://localhost:8000/sp-service/v1/shop/team/insert/team",
							"protocol": "http",
							"host": [
								"localhost"
							],
							"path": [
								"sp-service",
								"v1",
								"shop",
								"team",
								"insert",
								"team"
							],
							"port": "8000"
						}
					},
					"response": []
				}
			}
		},
		methods: {
			async handleDownload() {
				this.loading = true
				let arr = []
				for (let i = 20; i < this.formData.count + 20; i++) {
					let username = this.baseUserName + i;
					arr.push(await login(username, this.basePassword))
				}
				let res = await Promise.all(arr).catch(err => err)
				if (res instanceof Array) {
					let tokenArr = res.map(item => {
						console.log(item.data.data.jwt.access_token)
						return {
							token: item.data.data.jwt.access_token
						}
					})
					console.log(tokenArr)
					let name = this.itemTemplate.name
					let itemTemplateArr = tokenArr.map((tokenItem, index) => {
						
						this.itemTemplate.name = name + (index + 1)
						this.itemTemplate.request.auth.bearer[0].value = tokenItem.token
						this.itemTemplate.request.body.urlencoded = [{
							"key": "id",
							"value": this.formData.teamId,
							"type": "text"
						}]
						return JSON.parse(JSON.stringify(this.itemTemplate))
					})
					this.baseTemplate.item = itemTemplateArr
					this.baseTemplate.info._postman_id = Math.random().toString(36).substring(3, 8)
					console.log(this.baseTemplate)
					
					this.exportRaw('并发测试文件.json',this.baseTemplate)
				}
				this.loading = false
			},
			async getTeamListAlive() {
				let res = await getTeamListAlive()
				if (res.data.code == 200) {
					this.list = res.data.data.list
				}
			},
			fakeClick(obj) {
				var ev = document.createEvent("MouseEvents");
				ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
				obj.dispatchEvent(ev);
			},
			exportRaw(name, data) {
				var urlObject = window.URL || window.webkitURL || window;
				var export_blob = new Blob([JSON.stringify(data)], {
					type: 'application/json'
				});
				var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
				save_link.href = urlObject.createObjectURL(export_blob);
				save_link.download = name;
				this.fakeClick(save_link);
			}
		},
		async created() {
			await this.getTeamListAlive()
		}
	}
</script>

<style>
</style>
